#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtWidgets>
#include <string>

#include "xmlhelper.h"

#define XML_PATH QString("%1/config.xml").arg(QApplication::applicationDirPath())

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    OnInitDialog();
}

MainWindow::~MainWindow()
{
    delete ui;
    m_xml->saveFile(XML_PATH.toStdString());
}

void MainWindow::OnInitDialog()
{
    if(nullptr == m_xml)
        m_xml = std::make_shared<XmlHelper>();
    Q_ASSERT(m_xml);

    m_xml->loadFile(XML_PATH.toStdString());
    auto name = m_xml->read("student/name",QString("Bob").toStdString());
    auto age = m_xml->read("student/age",18);
    auto score = m_xml->read("student/score",99);
    auto is_male = m_xml->read("student/ismale",true);

    m_xml->write("student/name",std::string("BobLiu"));
    m_xml->write("student/grade",19);

    m_xml->debugPrintDocument();
    m_xml->saveFile(XML_PATH.toStdString());
    qDebug()<<"name: "<<QString::fromStdString(name)<<" age: "<<age<<" score: "<<score<<" ismale :"<<is_male<<"\r\n";
}


